import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { utils } from '@libs'

const app = createApp(App)
app.use(router)
app.use(store)

let instance = null
const render = (props = {}) => {
  const { container } = props
  instance = app.mount(container ? container.querySelector('#app') : '#app')
}

// eslint-disable-next-line
if (window.__POWERED_BY_QIANKUN__) {
  if (utils.env.isLocal) {
    // eslint-disable-next-line
    __webpack_public_path__ = `//localhost:${process.env.VUE_APP_PORT}`
  } else {
    // eslint-disable-next-line
    __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
  }
} else {
  render
}

export async function bootstrap() {
  console.log('vue app bootstraped')
}

export async function mount(props) {
  console.log('props from main framework', props)
  render(props)
}

export async function unmount() {
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
}
